package net.huashitong.ssydt.provider.user.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.provider.annotations.LoginRequired;
import net.huashitong.ssydt.provider.constant.SsydtConsts;
import net.huashitong.ssydt.provider.security.exceptions.ParamErrorException;
import net.huashitong.ssydt.provider.user.service.UserService;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UserUtils;
import net.huashitong.ssydt.user.entity.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author huiyang.yu
 */
@Api(value = "/api/users", tags = {"用户模块"})
@RestController
@RequestMapping("/api/users")
@Validated
public class ApiUserController {

    private UserService userService;

    private HttpServletRequest request;

    @Autowired
    public ApiUserController(UserService userService, HttpServletRequest request) {
        this.userService = userService;
        this.request = request;
    }

    @ApiOperation(value = "手机号（用户名）登录",
            notes = "1.调用用户中心的登录接口</br>2.写入Token信息</br>3.返回登录信息",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/login")
    public Result<LoginOutVO> login(@RequestBody @Valid LoginInVO loginInVO) {
        LoginOutVO loginOutVO = userService.login(loginInVO, request);
        return ResultUtils.getSuccessResults(loginOutVO);
    }

    @ApiOperation(value = "手机号注册",
            notes = "1.调用用户中心的注册接口</br>2.返回注册信息",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/register")
    public Result<String> register(@RequestBody @Valid RegisterVO registerVO) {
        userService.register(registerVO);
        return ResultUtils.getResults(Result.Status.SUCCESS, "注册成功");
    }


    @ApiOperation(value = "获取我的用户信息",
            notes = "1.用户对象接口</br>2.返回用户信息",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired//必须登录
    @GetMapping(value = "/my")
    public Result<UserOutVO> getUsersMy() {
        String userId = UserUtils.getUserId(); //获取登录用户id的方法
        UserOutVO userOutVO = userService.getByUserId(userId);
        return ResultUtils.getResults(Result.Status.SUCCESS, "", userOutVO);
    }

    @ApiOperation(value = "修改我的用户信息",
            notes = "1.调用用户中心修改基本信息接口</br>2.返回修改成功",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired//必须登录
    @PutMapping(value = "/my")
    public Result<String> putUsersMy(@RequestBody @Valid UserInVO userInVO) {
        String userId = UserUtils.getUserId(); //获取登录用户id的方法
        userService.updateUser(userId, userInVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "获取我的绑定信息",
            notes = "1.调用用户中心用户的绑定信息列表接口</br>2.返回成功",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired//必须登录
    @GetMapping(value = "/my/bindings")
    public Result<BindingsState> getMyBindings() {
        String userId = UserUtils.getUserId(); //获取登录用户id的方法
        BindingsState map = userService.getMyBindings(userId);
        return ResultUtils.getSuccessResults(map);
    }


    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/sms")
    public Result<String> sms(@RequestBody @Valid SmsVo smsVo) {
        userService.sms(smsVo, true);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "发送修改密码短信验证码",
            notes = "发送修改密码短信验证码",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping(value = "/my/sms")
    public Result<String> smsMy() {
        String userId = UserUtils.getUserId();
        userService.smsMy(userId);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "三方帐号注册", notes = "三方帐号注册", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/register/{thirdaccount}")
    public Result<String> thirdaccount(
            @ApiParam(value = "第三方帐号名称（WeChat, QQ, Weibo", required = true)
            @PathVariable(value = "thirdaccount") String thirdaccount,
            @RequestBody @Valid RegisterThirdAccount registerThirdAccount) {
        switch (thirdaccount) {
            case SsydtConsts.RegisterType.WE_CHAT:
            case SsydtConsts.RegisterType.QQ:
            case SsydtConsts.RegisterType.WEIBO:
                break;
            default:
                throw new ParamErrorException("第三方帐号名称错误");
        }
        registerThirdAccount.setThirdAccount(thirdaccount);
        userService.registerThirdAccount(registerThirdAccount);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "验证手机号是否注册",
            notes = "验证手机号是否注册，若results返回ture代表已经注册",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "mobile", value = "手机号码", paramType = "path", required = true)
    @GetMapping(value = "/mobile/{mobile}/isRegister")
    public Result<IsRegisterVO> mobileIsRegister(@PathVariable("mobile") String mobile) {
        IsRegisterVO vo = userService.mobileIsRegister(mobile);
        return ResultUtils.getSuccessResults(vo);
    }

    @ApiOperation(value = "发送语音验证码", notes = "发送语音验证码", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/sound")
    public Result<String> sound(@RequestBody @Valid SmsVo smsVo) {
        userService.sound(smsVo, true);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "发送修改密码语音验证码",
            notes = "发送修改密码语音验证码",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping(value = "/my/sound")
    public Result<String> soundMy() {
        String userId = UserUtils.getUserId();
        userService.soundMy(userId);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "微信登录", notes = "微信登录", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/login/wechat")
    public Result<LoginOutVO> loginWechat(@RequestBody @Valid LoginWechatVO loginWechatVO) {
        LoginOutVO loginOutVO = userService.loginWechat(loginWechatVO, request);
        return ResultUtils.getSuccessResults(loginOutVO);
    }

    @ApiOperation(value = "绑定微信", notes = "绑定微信", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping(value = "/wechat")
    public Result<String> bindingWeChat(@RequestBody @Valid BindingWeChatVO bindingWeChatVO) {
        userService.bindingWeChat(bindingWeChatVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "解绑微信", notes = "解绑微信", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping(value = "/wechat")
    public Result<String> deleteWeChat() {
        String userId = UserUtils.getUserId();
        userService.deleteWeChat(userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "QQ登录", notes = "QQ登录", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/login/qq")
    public Result<LoginOutVO> loginQq(@RequestBody @Valid LoginQqVO loginQqVO) {
        LoginOutVO loginOutVO = userService.loginQq(loginQqVO, request);
        return ResultUtils.getSuccessResults(loginOutVO);
    }

    @ApiOperation(value = "绑定QQ", notes = "1.校验验证码是否正常</br> 2.执行注册", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping(value = "/qq")
    public Result<String> bindingQq(@RequestBody @Valid BindingQqVO bindingQqVO) {
        userService.bindingQq(bindingQqVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "解绑QQ", notes = "解绑QQ", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping(value = "/qq")
    public Result<String> deleteQq() {
        String userId = UserUtils.getUserId();
        userService.deleteQq(userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "微博登录", notes = "微博登录", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/login/weibo")
    public Result<LoginOutVO> loginWeibo(@RequestBody @Valid LoginWeiboVO loginWeiboVO) {
        LoginOutVO loginOutVO = userService.loginWeibo(loginWeiboVO, request);
        return ResultUtils.getSuccessResults(loginOutVO);
    }

    @ApiOperation(value = "绑定微博", notes = "绑定微博", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping(value = "/weibo")
    public Result<String> bindingWeibo(@RequestBody @Valid BindingWeiboVO bindingWeiboVO) {
        userService.bindingWeibo(bindingWeiboVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "解绑微博", notes = "解绑微博", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping(value = "/weibo")
    public Result<String> deleteWeibo() {
        String userId = UserUtils.getUserId();
        userService.deleteWeibo(userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "更换（绑定）手机号", notes = "1.校验验证码是否正常</br> 2.若验证码正确执行修改密码", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PutMapping(value = "/my/mobile")
    public Result<String> mobileMy(@RequestBody @Valid MobileVO mobileVO) {
        String userId = UserUtils.getUserId();
        userService.mobileMy(userId, mobileVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "修改密码", notes = "修改密码", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PutMapping(value = "/my/password")
    public Result<String> passwordMy(@RequestBody @Valid PasswordMyVO passwordMyVO) {
        String userId = UserUtils.getUserId();
        userService.passwordMy(userId, passwordMyVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "重置密码", notes = "重置密码", produces = MediaType.APPLICATION_JSON_VALUE)
    @PutMapping(value = "/password")
    public Result<String> password(@RequestBody @Valid PasswordVO passwordVO) {
        userService.password(passwordVO);
        return ResultUtils.getSuccessResults();
    }


}
